﻿using Newtonsoft.Json;
using SmartlForm.DAL;
using SmartlForm.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SmartlForm.NetJs
{
    public partial class BowContainer
    {
        /// <summary>
        /// 获取关键词列表
        /// </summary>
        /// <returns></returns>
        public string getKeywordList(int pageNo, int pageSize, string keyword)
        {
            var list = KeywordDao.Instance.GetPageList(pageNo, pageSize, getAccountId(), keyword);
            return JsonConvert.SerializeObject(list);
        }

        /// <summary>
        /// 删除关键词
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result delKeyword(long id)
        {
            var keyword = KeywordDao.Instance.Get(id);
            if (keyword == null)
            {
                return ResultHelper.Error("关键词不存在！");
            }
            int x = KeywordDao.Instance.Del(id);
            if (x > 0)
            { 
                new Thread(new ParameterizedThreadStart(stopTask)).Start(keyword);
                return ResultHelper.Success("删除成功！");
            }
            else
            {
                return ResultHelper.Error("删除失败！");
            }
        }

        /// <summary>
        /// 添加关键词
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Result addKeyword(string name)
        {
            var keyword = KeywordDao.Instance.GetByName(name);
            if (keyword != null)
            {
                return ResultHelper.Error("此关键词已存在！");
            }
            long x = KeywordDao.Instance.Add(name, getAccountId());
            if (x > 0)
            {
                keyword = KeywordDao.Instance.GetByName(name);
                new Thread(new ParameterizedThreadStart(initTask)).Start(keyword);
                return ResultHelper.Success("保存成功！");
            }
            else
            {
                return ResultHelper.Error("保存失败！");
            }
        }
    }
}
